package com.jblv.frame.web.utils;



import com.jblv.frame.common.utils.SortListUtils;
import com.jblv.frame.user.api.vo.SysDeptListVO;
import com.jblv.frame.user.api.dto.SysDeptDTO;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author happyonion.yangtao
 * @version 1.0
 * @title DeptTreeUtils
 * @projectName jblv-frame
 * @packageName com.jblv.frame.common.utils
 * @description DeptTreeUtils
 * @date Create at 2020/6/3 9:30
 * @since jdk1.8
 */
public class DeptTreeUtils {

    private static DeptTreeUtils instance;

    public  List<SysDeptDTO> makeTree(List<SysDeptDTO> departmentList, String pId) {
        //子类
        List<SysDeptDTO> children = departmentList.stream().filter(x -> pId.equals(x.getParentId())).collect(Collectors.toList());
        //后辈中的非子类
        List<SysDeptDTO> successor = departmentList.stream().filter(x -> !pId.equals(x.getParentId()) ).collect(Collectors.toList());

        //排序子数据
        SortListUtils<SysDeptDTO> sortList = new SortListUtils<SysDeptDTO>();
        sortList.Sort(children, "getOrderNum", "asc");

        children.forEach(x ->
                {
                    makeTree(successor, x.getDeptId()).forEach(
                            y -> x.getChildren().add(y)
                    );
                }
        );

        return children;

    }

    public  List<SysDeptListVO> makeFacadeTree(List<SysDeptListVO> departmentList, String pId) {
        //子类
        List<SysDeptListVO> children = departmentList.stream().filter(x -> pId.equals(x.getParentId())).collect(Collectors.toList());
        //后辈中的非子类
        List<SysDeptListVO> successor = departmentList.stream().filter(x -> !pId.equals(x.getParentId())).collect(Collectors.toList());

        children.forEach(x ->
                {
                    makeFacadeTree(successor, x.getDeptId()).forEach(
                            y -> x.getChildren().add(y)
                    );
                }
        );

//        //排序子数据
//        SortListUtils<FacadeSysDeptDTO> sortList = new SortListUtils<FacadeSysDeptDTO>();
//        sortList.Sort(children, "getOrderNum", "asc");

        return children;

    }

    public static DeptTreeUtils getInstance(){
        if(instance == null){
            instance = new DeptTreeUtils();
        }
        return instance;

    }
    public static void main(String[] args) {
        List<SysDeptDTO> orderInfoBeans = new ArrayList<>();
        SysDeptDTO temp1 = new SysDeptDTO("1","1","0",19);
        SysDeptDTO temp2 = new SysDeptDTO("2","2","1",0);
        SysDeptDTO temp3 = new SysDeptDTO("3","3","1",5);
        SysDeptDTO temp4 = new SysDeptDTO("4","4","1",1);
        orderInfoBeans.add(temp1);
        orderInfoBeans.add(temp2);
        orderInfoBeans.add(temp3);
        orderInfoBeans.add(temp4);

        System.out.println("排序前:"+orderInfoBeans.toString());

        System.out.println("排序后:"+orderInfoBeans.toString());

    }

}
